package com.bygf.curve.demo;

import com.bygf.curve.axis.TimeAxis;
import com.bygf.curve.Curve;
import com.bygf.curve.TimeCurve;
import com.bygf.curve.XYCurve;
import com.bygf.curve.panel.ChartPanel;
import com.bygf.curve.utils.ToolTipFormat;
import java.awt.Font;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 *
 * @author sunyupei
 */
public class MemoryDemo extends javax.swing.JPanel {

    ChartPanel chartPanel = new ChartPanel();
    Timer timer = new Timer();
    List<Curve> curveList = new ArrayList<>();

    /**
     * Creates new form XYCurve
     */
    public MemoryDemo() {
        initComponents();
        chartPanel = new ChartPanel("内存实时使用（MB）", "time", "totalMemory(MB)", "", false);
        chartPanel.setBounds(0, 0, this.getWidth(), this.getHeight());
        TimeAxis timeAxis = new TimeAxis(chartPanel, "time");
        Date d1 = new Date(new Date().getTime() - 1000l * 60 * 60);
        Date d2 = new Date();
        timeAxis.setStartDate(d1);
        timeAxis.setEndDate(d2);
        timeAxis.setSimpleDateFormat(new SimpleDateFormat("HH:mm:ss"));
        chartPanel.setXAxis(timeAxis);
        chartPanel.getYAxis().setStartValue(0);
        chartPanel.getYAxis().setEndValue(Runtime.getRuntime().totalMemory() * 1F / 1000 / 1000);
        chartPanel.getTitlePanel().setTitleFont(new Font("宋体", Font.BOLD, 26));
        TimeCurve curve = new TimeCurve("内存使用");
        curveList.add(curve);
        chartPanel.getCurvePanel().setCurveList(curveList);
        curve.setToolTipFormat(new ToolTipFormat("{0}:{1},{2}", new SimpleDateFormat("HH:mm:ss"), new DecimalFormat("#.##MB")));
        this.add(chartPanel);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (curveList.isEmpty()) {
                    return;
                }
                TimeCurve curve = (TimeCurve) curveList.get(0);
                Date d1 = new Date();
                Date d0 = new Date(d1.getTime() - 1000l*60);
                curve.addItem(new Date(), (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) * 1F / 1000 / 1000);
                TimeAxis xAxis = (TimeAxis) chartPanel.getXAxis();
                xAxis.setStartDate(d0);
                xAxis.setEndDate(d1);
                chartPanel.getYAxis().setEndValue(Runtime.getRuntime().totalMemory() / 1000 / 1000);
                chartPanel.getCurvePanel().repaint();
            }
        }, 10, 500);
    }

    @Override
    public void setBounds(int x, int y, int width, int height) {
        super.setBounds(x, y, width, height);
        chartPanel.setBounds(0, 0, this.getWidth(), this.getHeight());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 547, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 387, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
